import { POST, GET } from '../axios.config.js'
import { sm2 } from 'sm-crypto'

const zbTenantId = 'KH'
const SM2_PK =
  '042cbefc1b6546d9e858482480c933e373960167594ea8be0c82491d6cf6d07512ec6e2354a3aacd24ad085c9eb4e8b34fe09ec8b4261f0925efb50d9cad85bda3'

const base = window.config.ip
const unifiedModule = 'public-uias-api'

const publicModule = `${base}/${unifiedModule}`
const user = `${base}/xn-supervise/user-manage`
const userBook = `${base}/xn-supervise/user-book`
const userPlay = `${base}/public-uias-back/uias-tenant`

const URLS = {
  internalLogin: `${publicModule}/uias_login/internalLogin`,
  internalEncLogin: `${publicModule}/uias_login/internalEncLogin`,
  loginUnified: `${publicModule}/uias_login/internalLogin`, // 账号+密码或者手机号+短信验证码登录
  loginUnifiedNew: `${publicModule}/identity-auth/gm/internalLogin`, // 双因子验证登录

  loginOut: `${publicModule}/uias_login/loginOut`,
  getBingPhoneCode: `${user}/getBingPhoneCode`, // 获取手机短信验证码
  modifyTelephone: `${user}/modifyTelephone`, // 修改手机号
  checkSmsCode: `${user}/checkSmsCode`, // 验证短信验证码
  updateLoginPassword: `${publicModule}/uias_login/updatePassword`, // 更新登录密码

  listUser: `${userBook}/listUser`, // 获取联络员列表
  addUserBook: `${userBook}/addUserBook`, // 添加联络员
  editUserBook: `${userBook}/editUserBook`, // 更新联络员信息
  delUserBook: `${userBook}/delUserBook`, // 删除联络员信息
  updateLoginStatus: `${userBook}/updateLoginStatus`, // 切换联络员
  getIdentifyingCode: `${publicModule}/uias_login/getIdentifyingCode`, // 获取验证码
  sendSignCodeByAliyun: `${publicModule}/identity-auth/sendSignCodeByAliyun`, // 手机验证码登录
  getRandomNumber: `${publicModule}/identity-auth/getRandomNumber`, // CA登录随机码接口
  getLoginRandomNumber: `${publicModule}/identity-auth/getLoginRandomNumber`, // 根据账号密码获取随机码
  verifyCertAuth: `${publicModule}/identity-auth/verifyCertAuth`,
  sendSignCode: `${publicModule}/uias_login/sendSignCodeByAliyun`, // 发送手机验证码
  userRegister: `${publicModule}/uias-index/skipValidation/userRegister`, // 用户注册接口
  getMenuByBelong: `${publicModule}/uias-menu/listRecursiveMenuByBelong`, // 查询所属页面下的菜单树开始
  listPageInfoByUserId: `${publicModule}/uias-menu/listPageInfoByUserId`, // 获取用户权限列表
  getTenantList: `${publicModule}/uias-tenant/getTenantList`, // 获取租户列表
  addNewTenant: `${userPlay}/addNewTenant`, // 用户注册
}

// 登录
export const internalEncLogin = ({ loginName, loginPassword, smCode = '' }) => {
  // const SM2_PK = '042cbefc1b6546d9e858482480c933e373960167594ea8be0c82491d6cf6d07512ec6e2354a3aacd24ad085c9eb4e8b34fe09ec8b4261f0925efb50d9cad85bda3'
  // const SM2_SPLITER = '___'
  const params = {
    appCode: 'A20230418170112123',
    loginType: 2, // 工作系统登录
    platType: 1,
    loginName: loginName,
    loginPassword: loginPassword,
    smCode,
    tenantId: zbTenantId,
  }
  // console.log(params)
  const encryptVal = '04' + sm2.doEncrypt(JSON.stringify(params), SM2_PK, 1)
  return POST(URLS.internalEncLogin, {
    oneParam: encryptVal,
  })
}

// 登录
export const loginUnified = ({ loginName, loginPassword = '', smCode = '' }) => {
  // const SM2_PK = '042cbefc1b6546d9e858482480c933e373960167594ea8be0c82491d6cf6d07512ec6e2354a3aacd24ad085c9eb4e8b34fe09ec8b4261f0925efb50d9cad85bda3'
  // const SM2_SPLITER = '___'
  // const encryptVal = '04' + sm2.doEncrypt(`${loginName}${SM2_SPLITER}${loginPassword}`, SM2_PK, 1)
  return POST(URLS.loginUnified, {
    appCode: 'A20230418170112123',
    loginType: 2, // 工作系统登录
    platType: 1,
    loginName: loginName,
    loginPassword: loginPassword,
    smCode: smCode,
    tenantId: zbTenantId,
  })
}

// 双因子加密登录（账号密码+手机号验证码）
export const loginUnifiedNew = ({ telephone = '', loginName = '', loginPassword = '', smCode = '' }) => {
  const params = {
    appCode: 'A20230418170112123',
    loginType: 2, // 工作系统登录
    platType: 1,
    loginName,
    loginPassword,
    telephone,
    smCode,
  }
  const encryptVal = '04' + sm2.doEncrypt(JSON.stringify(params), SM2_PK, 1)
  return POST(URLS.loginUnifiedNew, {
    oneParam: encryptVal,
    tenantId: zbTenantId,
  })
}

// 退出登录
export const logout = () => POST(URLS.loginOut)

// 获取手机短信验证码
// teyp 1.绑定：2.修改
// 修改手机号时传参 step( 1  2 ) 和 smsCode
export const getBingPhoneCode = (type, mobile, { step, smsCode } = {}) =>
  POST(URLS.getBingPhoneCode, {
    type,
    mobile,
    step,
    smsCode,
  })

// 更新手机号码
// teyp 1.绑定：2.修改
export const modifyTelephone = (newPhone, smCode, type, oldSmCode) =>
  POST(URLS.modifyTelephone, {
    newPhone,
    smCode,
    type,
    oldSmCode,
  })

// 验证短信验证码
export const checkSmsCode = (mobile, smsCode) =>
  POST(URLS.checkSmsCode, {
    mobile,
    smsCode,
  })

// 更新登录密码
// type 改密方式 1.原密码修改 2. 通过手机验证码修改
export const updateLoginPassword = ({ oldPwd, newPwd, repeatPwd }) =>
  POST(URLS.updateLoginPassword, {
    oldPwd,
    newPwd,
    repeatPwd,
    type: 1,
    tenantId: zbTenantId,
  })

// 忘记密码
export const retrieveLoginPassword = ({ telephone, smCode, password, repeatPwd }) =>
  POST(URLS.updateLoginPassword, {
    telephone,
    smCode,
    newPwd: password,
    repeatPwd,
    tenantId: '',
    type: 2,
  })

// 获取联络员列表
export const listUser = (isCurrLogin = false) =>
  POST(URLS.listUser, {
    status: (isCurrLogin && '1') || '',
  })

// 添加联络员
export const addUserBook = ({ userName, post, email, phone, smsCode }) =>
  POST(URLS.addUserBook, {
    userName,
    post,
    email,
    phone,
    smsCode,
    userType: '3',
  })

// 编辑联络员
export const editUserBook = ({ id, userName, post, email, phone, smsCode }) =>
  POST(URLS.editUserBook, {
    id,
    userName,
    post,
    email,
    phone,
    smsCode,
  })

// 删除联络员列表
export const delUserBook = id =>
  POST(URLS.delUserBook, {
    id,
  })

// 切换联络员
export const changeUserBook = id =>
  POST(URLS.updateLoginStatus, {
    id,
  })

// 获取验证码
// check 0不校验 1校验
// receiveType 发送类型 1 校验手机号如果存在，不发送验证码
// type: cryun-创瑞云短信平台；aliyun-阿里云短信平台
export const getIdentifyingCode = mobile =>
  POST(
    URLS.getIdentifyingCode,
    {
      oneParam: mobile,
      check: '0',
      receiveType: '1',
      sendType: '1',
      tenantId: zbTenantId,
    },
    {
      headers: {
        hideLoading: true,
      },
    }
  )

// 获取手机短信验证码
export const sendSignCodeByAliyun = ({ mobile }) => {
  return POST(URLS.sendSignCodeByAliyun, {
    oneParam: mobile,
    check: '1',
    receiveType: '0',
    type: 'aliyun',
    tenantId: zbTenantId,
  })
}

// CA登录获取随机数
export const getRandomNumber = () =>
  POST(URLS.getRandomNumber, '', {
    headers: {
      hideLoading: true,
    },
  })

// 账号密码+CA登录获取随机数
export const getLoginRandomNumber = ({ loginPassword = '', loginName = '' }) => {
  const params = {
    loginPassword,
    loginName,
  }
  const encryptVal = '04' + sm2.doEncrypt(JSON.stringify(params), SM2_PK, 1)
  return POST(
    URLS.getLoginRandomNumber,
    { oneParam: encryptVal },
    {
      headers: {
        hideLoading: true,
      },
    }
  )
}

// USB_KEY登录验证
export const verifyCertAuth = ({ randomNum = '', strSignData = '', strUkeySn = '', certdata = '' }) =>
  POST(
    URLS.verifyCertAuth,
    {
      randomNum,
      strSignData,
      strUkeySn,
      strCert: certdata,
    },
    {
      headers: {
        hideLoading: true,
      },
    }
  )

// 发送手机验证码
// check  校验账号是否存在 0不校验 1校验
export const sendSignCode = (mobile, check) =>
  POST(
    URLS.sendSignCode,
    {
      check,
      oneParam: mobile,
      sendType: '1',
      tenantId: '',
    },
    {
      headers: {
        hideLoading: true,
      },
    }
  )

// 注册用户
export const userRegister = ({ accountName = '', telephone = '', smsCode = '', loginPassword = '' }) =>
  POST(URLS.userRegister, {
    accountName,
    loginPassword,
    smsCode,
    telephone,
    tenantId: zbTenantId,
  })

// 查询所属页面下的菜单树开始
export const getMenuByBelong = type => GET(URLS.getMenuByBelong, { belongPage: type })

// 查询所属页面下的菜单树开始
export const getUserpermissions = userId => POST(URLS.listPageInfoByUserId, { userId: userId })

// 获取租户列表
export const getTenantList = tenantId => POST(URLS.getTenantList, { tenantId })

// 登录
export const internalLogin = ({ loginName = '', loginPassword = '', smCode = '', loginType = '' }) => {
  const params = {
    appCode: 'jx-clivil-servant-assessment',
    loginType: loginType, // 工作系统登录
    platType: 2,
    currentUserId: '',
    loginName: loginName,
    loginPassword: loginPassword,
    smCode: smCode,
    tenantId: localStorage.pskh_tenantId || '',
  }
  // console.log(params)
  return POST(URLS.internalLogin, params)
}

// 注册用户
export const addNewTenant = ({ orgName = '', telephone = '', code = '', password = '' }) =>
  POST(URLS.addNewTenant, {
    orgName,
    password,
    code,
    telephone,
  })
